package org.study.module.hadoop;

import org.study.module.common.AbstractConfigBuilder;
import org.study.module.common.ModuleCommon;

import java.util.ArrayList;
import java.util.List;

/**
 * Hadoop配置构建
 * core-site.xml
 */
public class HadoopConfigBuilder extends AbstractConfigBuilder {


    /**
     * 添加集群服务名称
     *
     * @param fs 服务名称：
     */
    public HadoopConfigBuilder fs(String fs) {
        addProperty("集群服务名", "fs.defaultFS", "hdfs://" + fs);
        return this;
    }

    /**
     * 添加Hadoop临时目录
     *
     * @param tmpDir 临时目录
     */
    public HadoopConfigBuilder tmpDir(String tmpDir) {
        addMessage("Hadoop临时目录");
        addProperty("hadoop.tmp.dir", tmpDir);
        return this;
    }

    /**
     * 启用Kerberos认证,并启用服务级认证（服务间通信加密）
     */
    public HadoopConfigBuilder securityByKerberos(String realm) {
        addMessage("启用Kerberos认证");
        addProperty("hadoop.security.authentication", "kerberos");
        addMessage(" 启用服务级认证（服务间通信加密）");
        addProperty("hadoop.security.authorization", "true");
        addMessage("启用服务级认证（服务间通信加密）");
        addProperty("hadoop.security.auth_to_local", "RULE:[2:$1/$2@$0]([ndj]n/.*@" + realm + ")s/.*/hdfs/\n" +
                "    RULE:[2:$1/$2@$0]([rn]m/.*@" + realm + ")s/.*/yarn/\n" +
                "    RULE:[2:$1/$2@$0](jhs/.*@" + realm + ")s/.*/mapred/\n" +
                "    DEFAULT");
        return this;
    }


    /**
     * 基于Zookeeper实现高可用，Zookeeper地址，过个地址用逗号隔开
     *
     * @param zookeeperHosts Zookeeper地址： host1:2181,host2:2181
     */
    public HadoopConfigBuilder zookeeperHosts(String zookeeperHosts) {
        addMessage("Zookeeper地址");
        addProperty("ha.zookeeper.quorum", zookeeperHosts);
        return this;
    }

    public HadoopConfigBuilder ladp(String host, String user, String password, String groups) {
        addMessage("LDAP组映射类");
        addProperty("hadoop.security.group.mapping", "org.apache.hadoop.security.LdapGroupsMapping");
        addMessage("LDAP服务器地址");
        addProperty("hadoop.security.group.mapping.ldap.url", "ldap://" + host);
        addMessage("LDAP绑定账号");
        addProperty("hadoop.security.group.mapping.ldap.bind.user", user);
        addMessage("LDAP绑定密码");
        addProperty("hadoop.security.group.mapping.ldap.bind.password", password);
        addMessage("组搜索基础DN");
        addProperty("hadoop.security.group.mapping.ldap.base", groups);
        addMessage(" 用户过滤条件");
        addProperty("hadoop.security.group.mapping.ldap.search.filter.user", "(&(objectClass=posixAccount)(uid={0}))");
        addMessage(" 组过滤条件");
        addProperty("hadoop.security.group.mapping.ldap.search.filter.group", "(&(objectClass=posixGroup)(memberUid={0}))");
        return this;
    }

    @Override
    public void addConfigHead() {
        configs.add("cat > $HADOOP_HOME/etc/hadoop/core-site.xml << EOF");
        super.addConfigHead();
    }

    public HadoopConfigBuilder proxyUserHosts(String user) {
        addProperty("hadoop.proxyuser." + user + ".hosts", "*");
        return this;
    }

    public HadoopConfigBuilder proxyUserGroups(String user) {
        addProperty("hadoop.proxyuser." + user + ".groups", "*");
        return this;
    }
}
